package edu.jmu.dustbinMonitoringSystem.dto;

import com.google.common.collect.Lists;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @author Wen
 * 请求成功工具
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class SuccessResult<T extends AbstractBaseDomain> extends AbstractBaseResult {
    private Links links;
    private List<DataBean> data;

    public SuccessResult(String self,int next,int last,List<T> attributes){
        links = new Links();
        links.setSelf(self);
        links.setNext(self+"?page="+next);
        links.setLast(self+"?page="+last);

        attributes.forEach(attribute->createDataBen(self,attribute));

    }

    public SuccessResult(String self,T attributes){
        links = new Links();
        links.setSelf(self);

        createDataBen(null,attributes);


    }
    public void createDataBen(String self,T attributes ){
        if (data==null){
            data = Lists.newArrayList();
        }
        DataBean dataBean = new DataBean();
        dataBean.setId(attributes.getId());
        dataBean.setType(attributes.getClass().getSimpleName());
        dataBean.setAttributes(attributes);

        if (StringUtils.isNoneBlank(self)){
            Links links  = new Links();
            links.setSelf(self+"/"+attributes.getId());
            dataBean.setLinks(links);
        }
        data.add(dataBean);
    }


}
